package com.demostic.demostic_test.utils;

import android.content.Context;
import android.widget.TextView;
import android.widget.TimePicker;

import com.accloud.cloudservice.SecuritySession;
import com.demostic.demostic_test.R;

import org.java_websocket.drafts.Draft_75;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {
    private static final String FORMAT = "HH:mm";
    private static final String TAG = TimeUtil.class.getSimpleName();
    static ArrayList<Byte> exitWeekList = new ArrayList<>();
    static ArrayList<Byte> numList = new ArrayList<>();

    public static String getWeek(Context context, byte b) {
        StringBuilder sb = new StringBuilder();
        if ((b & 1) == 1) {
            sb.append(context.getString(R.string.clock_aty_time_mon));
        }
        if ((b & 2) == 2) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_tues));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_tues_));
            }
        }
        if ((b & 4) == 4) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_wed));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_wed_));
            }
        }
        if ((b & 8) == 8) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_thur));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_thur_));
            }
        }
        if ((b & 16) == 16) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_fri));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_fri_));
            }
        }
        if ((b & 32) == 32) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_sta));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_sta_));
            }
        }
        if ((b & 64) == 64) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_sun));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_sun_));
            }
        }
        if ((b & 128) == 128) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_once));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_once_));
            }
        }
        return sb.toString();
    }

    public static String getWeek_780(Context context, byte b) {
        StringBuilder sb = new StringBuilder();
        if ((b & 1) == 1) {
            sb.append(context.getString(R.string.clock_aty_time_every_mon));
        }
        if ((b & 2) == 2) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_every_tues));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_every_tues_));
            }
        }
        if ((b & 4) == 4) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_every_wed));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_every_wed_));
            }
        }
        if ((b & 8) == 8) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_every_thur));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_every_thur_));
            }
        }
        if ((b & 16) == 16) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_every_fri));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_every_fri_));
            }
        }
        if ((b & 32) == 32) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_every_sta));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_every_sta_));
            }
        }
        if ((b & 64) == 64) {
            if (sb.length() == 0) {
                sb.append(context.getString(R.string.clock_aty_time_every_sun));
            } else {
                sb.append(context.getString(R.string.clock_aty_time_every_sun_));
            }
        }
        return sb.toString();
    }

    public static void getExitWeekList(ArrayList<Byte> exitWeekList2, byte b) {
        if (exitWeekList2 != null) {
            if ((b & 1) == 1 && !exitWeekList2.contains((byte) 1)) {
                exitWeekList2.add((byte) 1);
            }
            if ((b & 2) == 2 && !exitWeekList2.contains((byte) 2)) {
                exitWeekList2.add((byte) 2);
            }
            if ((b & 4) == 4 && !exitWeekList2.contains((byte) 4)) {
                exitWeekList2.add((byte) 4);
            }
            if ((b & 8) == 8 && !exitWeekList2.contains((byte) 8)) {
                exitWeekList2.add((byte) 8);
            }
            if ((b & 16) == 16 && !exitWeekList2.contains((byte) 16)) {
                exitWeekList2.add((byte) 16);
            }
            if ((b & 32) == 32 && !exitWeekList2.contains((byte) 32)) {
                exitWeekList2.add((byte) 32);
            }
            if ((b & 64) == 64 && !exitWeekList2.contains(64)) {
                exitWeekList2.add((byte) 64);
            }
            if ((b & 128) == 128 && !exitWeekList2.contains(Byte.MIN_VALUE)) {
                exitWeekList2.add(Byte.MIN_VALUE);
            }
        }
    }

    public static ArrayList<Byte> getExitWeekLists(byte b) {
        if (exitWeekList == null) {
            return null;
        }
        if ((b & 1) == 1 && !exitWeekList.contains((byte) 1)) {
            exitWeekList.add((byte) 1);
        }
        if ((b & 2) == 2 && !exitWeekList.contains((byte) 2)) {
            exitWeekList.add((byte) 2);
        }
        if ((b & 4) == 4 && !exitWeekList.contains((byte) 4)) {
            exitWeekList.add((byte) 4);
        }
        if ((b & 8) == 8 && !exitWeekList.contains((byte) 8)) {
            exitWeekList.add((byte) 8);
        }
        if ((b & 16) == 16 && !exitWeekList.contains((byte) 16)) {
            exitWeekList.add((byte) 16);
        }
        if ((b & 32) == 32 && !exitWeekList.contains((byte) 32)) {
            exitWeekList.add((byte) 32);
        }
        if ((b & 64) == 64 && !exitWeekList.contains(64)) {
            exitWeekList.add((byte) 64);
        }
        if ((b & 128) == 128 && !exitWeekList.contains(Byte.MIN_VALUE)) {
            exitWeekList.add(Byte.MIN_VALUE);
        }
        return exitWeekList;
    }

    public static int getHour(byte data) {
        return data & Draft_75.END_OF_FRAME;
    }

    public static int getMinute(byte data) {
        return data & Draft_75.END_OF_FRAME;
    }

    public static String Local2UTC(byte hour, byte minute) {
        SimpleDateFormat utcSdf = new SimpleDateFormat(FORMAT);
        SimpleDateFormat localSdf = new SimpleDateFormat(FORMAT);
        utcSdf.setTimeZone(TimeZone.getTimeZone("gmt"));
        localSdf.setTimeZone(TimeZone.getDefault());
        try {
            return utcSdf.format(localSdf.parse(((int) hour) + ":" + ((int) minute)));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date parseDate(String date) {
        try {
            return new SimpleDateFormat(FORMAT).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date changeTimeZone(String str, TimeZone oldZone, TimeZone newZone) {
        Date date = parseDate(str);
        if (date == null) {
            return null;
        }
        return new Date(date.getTime() - ((long) (oldZone.getRawOffset() - newZone.getRawOffset())));
    }

    public static String genExistTime(byte hour, byte minute) {
        Date date = changeTimeZone((hour < 10 ? "0" + ((int) hour) : "" + ((int) hour)) + ":" + (minute < 10 ? "0" + ((int) minute) : "" + ((int) minute)), TimeZone.getTimeZone("GMT"), TimeZone.getDefault());
        int newHour = date.getHours();
        int newMinute = date.getMinutes();
        return (newHour < 12 ? "0" + newHour : "" + newHour) + ":" + (newMinute < 12 ? "0" + newMinute : "" + newMinute);
    }

    public static String genExistTime_(byte hour, byte minute) {
        return (hour < 10 ? "0" + ((int) hour) : "" + ((int) hour)) + ":" + (minute < 10 ? "0" + ((int) minute) : "" + ((int) minute));
    }

    public static String getShowTime(int hour, int minute) {
        return (hour < 10 ? "0" + hour : "" + hour) + ":" + (minute < 10 ? "0" + minute : "" + minute);
    }

    public static long getMinuteTime(TimeZone timeZone) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        sdf.setTimeZone(timeZone);
        try {
            return (new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(sdf.format(new Date())).getTime() / 1000) / 60;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static int getLocalTime(int hour, int minute) {
        return (hour * 60) + minute;
    }

    public static byte[] getTimeBytes() {
        int dayOfWeek;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getDefault());
        int year = calendar.get(1);
        int month = calendar.get(2) + 1;
        int dayOfMonth = calendar.get(5);
        int dayOfWeek2 = calendar.get(7);
        if (dayOfWeek2 == 1) {
            dayOfWeek = 7;
        } else {
            dayOfWeek = dayOfWeek2 - 1;
        }
        return new byte[]{(byte) ((65280 & year) >> 8), (byte) (year & 255), (byte) month, (byte) dayOfMonth, (byte) dayOfWeek, (byte) calendar.get(11), (byte) calendar.get(12), (byte) calendar.get(13)};
    }

    public static ArrayList<Byte> getNumByWeek(byte b) {
        numList.clear();
        if ((b & 1) == 1) {
            numList.add((byte) 1);
        }
        if ((b & 2) == 2) {
            numList.add((byte) 2);
        }
        if ((b & 4) == 4) {
            numList.add((byte) 3);
        }
        if ((b & 8) == 8) {
            numList.add((byte) 4);
        }
        if ((b & 16) == 16) {
            numList.add((byte) 5);
        }
        if ((b & 32) == 32) {
            numList.add((byte) 6);
        }
        if ((b & 64) == 64) {
            numList.add((byte) 7);
        }
        return numList;
    }

    public static ArrayList<Integer> getClockRoomID(byte b) {
        ArrayList<Integer> list = new ArrayList<>();
        if ((b & 1) == 1) {
            list.add(11);
        }
        if ((b & 2) == 2) {
            list.add(12);
        }
        if ((b & 4) == 4) {
            list.add(13);
        }
        if ((b & 8) == 8) {
            list.add(14);
        }
        if ((b & 16) == 16) {
            list.add(15);
        }
        if ((b & 32) == 32) {
            list.add(16);
        }
        if ((b & 64) == 64) {
            list.add(17);
        }
        if ((b & 128) == 128) {
            list.add(18);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 256) {
            list.add(19);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 512) {
            list.add(20);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 1024) {
            list.add(21);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 2048) {
            list.add(22);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 4096) {
            list.add(23);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 8192) {
            list.add(24);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 16384) {
            list.add(25);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 32768) {
            list.add(26);
        }
        return list;
    }

    public static ArrayList<Integer> getClockRoomID_(byte b) {
        ArrayList<Integer> list = new ArrayList<>();
        if ((b & 1) == 1) {
            list.add(1);
        }
        if ((b & 2) == 2) {
            list.add(2);
        }
        if ((b & 4) == 4) {
            list.add(4);
        }
        if ((b & 8) == 8) {
            list.add(8);
        }
        if ((b & 16) == 16) {
            list.add(16);
        }
        if ((b & 32) == 32) {
            list.add(32);
        }
        if ((b & 64) == 64) {
            list.add(64);
        }
        if ((b & 128) == 128) {
            list.add(128);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 256) {
            list.add(256);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 512) {
            list.add(512);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 1024) {
            list.add(1024);
        }
        if ((b & SecuritySession.SECURITY_TYPE_NONE) == 2048) {
            list.add(2048);
        }
        return list;
    }

    public static void setTimePickerTime(final Context context, TimePicker timePicker, final String timeMode, final TextView tv_time, final TextView tv_pm) {
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            /* class com.demostic.demostic_test.utils.TimeUtil.AnonymousClass1 */

            @Override // android.widget.TimePicker.OnTimeChangedListener
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                String string;
                if (timeMode.equals("24")) {
                    tv_time.setText(TimeUtil.getShowTime(hourOfDay, minute));
                    TextView textView = tv_pm;
                    if (hourOfDay >= 12) {
                        string = context.getString(R.string.clock_aty_pm);
                    } else {
                        string = context.getString(R.string.clock_aty_am);
                    }
                    textView.setText(string);
                } else if (!timeMode.equals("12")) {
                } else {
                    if (hourOfDay > 12) {
                        tv_time.setText(TimeUtil.getShowTime(hourOfDay - 12, minute));
                        tv_pm.setText(context.getString(R.string.clock_aty_pm));
                        return;
                    }
                    tv_time.setText(TimeUtil.getShowTime(hourOfDay, minute));
                    tv_pm.setText(hourOfDay == 12 ? context.getString(R.string.clock_aty_pm) : context.getString(R.string.clock_aty_am));
                }
            }
        });
    }
}
